package xin.jishu.ai.managers;

import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import xin.jishu.ai.entities.Student;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sxsx欧克 <wo@jishu.xin>
 */
public class StudentManager {

    private XSSFWorkbook workbook;

    @SneakyThrows
    public void initialize(Path source) {
        this.workbook = new XSSFWorkbook(
                source.toFile()
        );
    }

    public List<Student> resolve() {
        List<Student> result = new ArrayList<>();

        for (Sheet sheets : this.workbook) {
            for (Row row : sheets) {
                if (row.getCell(0) == null) {
                    break;
                }

                Student student = Student.builder()
                        .id(
                                Long.parseLong(
                                        row.getCell(0)
                                                .getStringCellValue()
                                )
                        )
                        .name(
                                row.getCell(1)
                                        .getStringCellValue()
                        )
                        .className(
                                row.getCell(2)
                                        .getStringCellValue()
                        )
                        .phone(
                                Long.parseLong(
                                        row.getCell(3)
                                                .getStringCellValue()
                                )
                        )
                        .build();

                result.add(student);
            }
        }

        return result;
    }

}
